package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.*;
import com.example.jyjava.dao.OrderDao;
import com.example.jyjava.dao.ShopDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ShopDao shopDao;

    @RequestMapping("/addOrder")
    public String addOrder(@RequestBody Order order) {
        String falg = "400";
        order.setUpdateTime(new Date());
        order.setCreatTime(new Date());
        order.setDeleteType(false);
        int i = orderDao.addOrder(order);
        int b = shopDao.updateShopBuy(order.getShopId());
        HashMap<String, Object> res = new HashMap<>();  //map封装

        if (i > 0 && b > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("order", i);
            res.put("shop", b);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getAllOrder")
    public String getAllOrder(@RequestBody Order order) {
        String falg = "400";
        List<Order> listOne = new ArrayList<Order>();
        if (Integer.valueOf(order.getOrderType()) == 3) {
            if (order.getUserId() != null && order.getSellerId() == null) {
                List<Order> orderList1 = orderDao.getAllOrder(order.getUserId(), "3", order.getSellerId());
                listOne.addAll(orderList1);
            } else if (order.getUserId() == null && order.getSellerId() != null) {
                List<Order> orderList1 = orderDao.getAllOrder(order.getUserId(), "3", order.getSellerId());
                List<Order> orderList2 = orderDao.getAllOrder(order.getUserId(), "6", order.getSellerId());
                listOne.addAll(orderList1);
                listOne.addAll(orderList2);
            }
        } else {
            List<Order> orderList = orderDao.getAllOrder(order.getUserId(), order.getOrderType(), order.getSellerId());
            listOne.addAll(orderList);
        }
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (listOne != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("orderList", listOne);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/updateOrderType")
    public String updateOrderType(@RequestBody Order order) {
        String falg = "400";

        if (Integer.valueOf(order.getOrderType()) == 4) {
            int b = shopDao.xianShopBuy(order.getShopId());
        }
        if (Integer.valueOf(order.getOrderType()) == 5) {
            int b = shopDao.xianShopBuy(order.getShopId());
        }
        if (Integer.valueOf(order.getOrderType()) == 11) {
            int b = shopDao.xianShopBuy(order.getShopId());
        }
        int i = orderDao.updateOrderType(order.getId(), order.getOrderType());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (i > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("orderList", i);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getOrderId")
    public String getOrderId(@RequestBody Order order) {
        String falg = "400";
        Order or = orderDao.getOrderId(order.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (or != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("orderList", or);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getOrder")
    public String getOrder(ShopQueryInfo shopQueryInfo) {
        String falg = "400";
        int numbers = orderDao.getOrderC("%" + shopQueryInfo.getQuery() + "%", shopQueryInfo.getUserId(), shopQueryInfo.getSellerId());
        int pageStart = (shopQueryInfo.getPageNum() - 1) * shopQueryInfo.getPageSize();
        List<Order> orderList = orderDao.getOrder("%" + shopQueryInfo.getQuery() + "%", pageStart, shopQueryInfo.getPageSize(), shopQueryInfo.getUserId(), shopQueryInfo.getSellerId());
        HashMap<String, Object> res = new HashMap<>();
        if (orderList != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("orderList", orderList);
            res.put("number", numbers);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }
}
